//数据类型 1:单品；2：列表页(根据分类)； 3:分类页；4：新闻列表页  ；5：新闻页 ；6：商品类型；7：品牌分类 ；9：关键字；10:自定义url
function toLink(dataType, dataId,customUrl,indexClassDataId) {
	var skuBaseUrl = "/goods/{goodsId}.html";
	var goodsClassifiedBaseUrl = "/goods/findPageResult.html?goodsClassId={goodsClassId}";
	var categoryBaseUrl = "/goodsClass/{selectedClassId}.html";
	var newsClassBaseUrl = "/article/article.html?articleClassId={articleClassId}";
	var newsDetailBaseUrl = "/article/detail/{newsId}.html";
	//6：商品类型
	var listWithTypeUrl = "/goods/findPageResult.html?typeClass={typeClass}";
	//7：品牌
	var listWithBrandUrl = "/goods/findPageResult.html?brandId={brandId}&goodsClassId={goodsClassId}";
	//9：关键字
	var searchUrl = "/goods/findPageResult.html?keyWord=${keyWord}";
	var url = null;
	var dataTypeInt = 0;
	if((dataType+"").match(/^\d+$/)){
		dataTypeInt = parseInt(dataType);
	}
	switch (dataTypeInt) {
	case 1:
		url = skuBaseUrl.replace("{goodsId}", dataId);
		break;
	case 2:
		url = goodsClassifiedBaseUrl.replace("{goodsClassId}", dataId);
		break;
	case 3:
		url = categoryBaseUrl.replace("{selectedClassId}", dataId);
		break;
	case 4:
		url = newsClassBaseUrl.replace("{articleClassId}", dataId);
		break;
	case 5:
		url = newsDetailBaseUrl.replace("{newsId}", dataId);
		break;
	case 6:
		url = listWithTypeUrl.replace("{typeClass}", dataId);
		break;
	case 7:
		url = listWithBrandUrl.replace("{brandId}", dataId);
		if(indexClassDataId){
			url = url.replace("{goodsClassId}", indexClassDataId);
		}else{
			url = url.replace("{goodsClassId}", "");
		}
		break;
	case 9:
		url = searchUrl.replace("{keyWord}", dataId);
		break;
	case 10:
		url = customUrl;
		break;
	default:
		url = "/index.html";
		break;
	}
	window.location.href = (10==dataType?'':appPath)+url;
}
function readyDo(){
	$(".swiper-container").swiper({
		loop : true,
		autoplay : 3000
	});
	//$("#space_header_div").css("height",$("#header_div").height());
	$("#space_footer_div").css("height",$("#footer_div").height());
	//
	//var playerHeight = $(".index-pic img").height();
	/*
	$(".prism-player").each(function(){
		var fileSource = $(this).attr("dataurl");
		var containerId = $(this).attr("id");
		var coverurl = $(this).attr("coverurl");
		var heightdate = $(this).attr("heightdate");
		var player = new prismplayer({
			id: containerId, // 容器id
			source: fileSource,  // 视频url 支持互联网可直接访问的视频地址
			//source: "http://player.ku6.com/inside/XGZadFw5voEoeHNbH4c4Gg../v.swf&adss=0",
			autoplay: false,      // 自动播放
			width: "auto",       // 播放器宽度
			height: heightdate,      // 播放器高度
			cover: coverurl,
			skinLayout: [
			             {
			            	 "align":"cc",
			            	 "x":30,
			            	 "y":80,
			            	 "name":"bigPlayButton"
			             }
			             ]
		});
	});*/
	$("#search_cancel").click(function(){
		cancleSearch();
	});
	$(window).scroll(function(){
	    var sc=$(window).scrollTop();
	    var rwidth=$(window).width()
	    if(sc>0){
	       $("#goTopBtn").css("display","block");
	       $("#goTopBtn").css("left",(rwidth-60)+"px")
	    }else{
	     $("#goTopBtn").css("display","none");
	    }
	 })
	 $("#goTopBtn").click(function(){
	    var sc=$(window).scrollTop();
	    $('body,html').animate({scrollTop:0},500);
	 })
	// 判断微信浏览器
	if (is_wx()) {
		//隐藏注销
		$(".wx-hidden-a").hide();
	} 
}
function logout(){
	$.ajax({
		type : "post",
		url : appPath + "/setting/logout.html",
		data : {
			token : $.cookie("token")
		},
		success : function(result) {
			$.cookie('token', null, {
				path : '/',
				domain : '.xinghaoshangcheng.com',
				expires : -1
			});
			location.replace(appPath + "/index.html");
			//window.location = appPath + "/member/login.html";
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			$.alert("系统异常，请稍后再试");
		}
	});
}
/**
 * 显示搜索框
 */
function showSearchBar(){
	//$("#search_div").show();
	$("#search_div").popup();
	$("#search_text").click();
}
function cancleSearch(){
	//$("#search_div").closePopup();
	$("#search_div").hide();
}

function is_wx(){
	// 判断微信浏览器
	var ua = navigator.userAgent.toLowerCase();
	if (ua.match(/MicroMessenger/i) == "micromessenger") {
		return true;
	} 
	return false;
}
$(document).ready(function(){
	if(is_wx()){
		$("img.lazy-img").lazyload({
			event : "sporty"
		});
		$(window).bind("load", function() {
			//var timeout = setTimeout(function() {
			$("img.lazy-img").trigger("sporty");
			//}, 200);
		});
	}else{
		 // 初始化的方式
		$("img.lazy-img").lazyload({
		    threshold : 100
		});
	}
	$.ajax({
		url : appPath + '/getIndexHeader.html',
		type : 'POST',
		data : {
			token : $.cookie("token")
		},
		dataType : 'html',
		success : function(htmlData) {
			var div =  $(htmlData).find("#title_div");
			var appendHtml = $(div[0]).html();
			$("#title_div").html(appendHtml);
			readyDo();
		},
		error : function(result) {
			$.alert("系统异常！");
		}
	});
	red();
});

function timer(intDiff) {
	var t1 = setInterval(function() {
		var day = 0, hour = 0, minute = 0, second = 0;// 时间默认值
		if (intDiff > 0) {
			hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
			minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
			second = Math.floor(intDiff) - (day * 24 * 60 * 60)
					- (hour * 60 * 60) - (minute * 60);
		} else {
			$('#nobegin').hide();
			$('#begin').show();
			clearTimeout(t1);
		}
		if (minute <= 9)
			minute = '0' + minute;
		if (second <= 9)
			second = '0' + second;

		$('#hour_show').html('<s id="h"></s>' + hour + '时');
		$('#minute_show').html('<s></s>' + minute + '分');
		$('#second_show').html('<s></s>' + second + '秒');
		intDiff--;
	}, 1000);
}

function red(){
	var begin = new Date();
	$.ajax({
		url : appPath + '/red/getTime.json',
		dataType : 'json',
		type : 'post',
		success : function(data) {
			if (data.time1 != null && data.time1 != undefined) {
				timer(Math.round(data.time1
						- (new Date().valueOf() - begin.valueOf() - data.com))/1000);
				$('#hongbao').show();
				$('#hongbaoTime').show();
			}else{
				$('#hongbao').show();
			}			
		},
		error : function() {
			$.alert("系统异常，请重试");
		}
	})
}